/*******************************************************************************
 * Copyright (c) 2005, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package us.gaaoc.framework.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;

import us.gaaoc.framework.model.CaseRecord;
import us.gaaoc.framework.model.CaseRecordIdExternalCaseRecordNumber;
import us.gaaoc.framework.model.Organizations;

public class CaseRecordExternalCaseRecordNumberDAOHibernate extends GenericHibernateDAO<CaseRecordIdExternalCaseRecordNumber, String, CaseRecordExternalCaseRecordNumberDAO> implements
CaseRecordExternalCaseRecordNumberDAO {

	public void insertIntoCaseRecordIdExternalCaseRecordNumber(CaseRecord caseRecord, String externalCaseRecordUuid, Organizations org) {
		CaseRecordIdExternalCaseRecordNumber cs = new CaseRecordIdExternalCaseRecordNumber();
		cs.setCaseRecord(caseRecord);
		cs.setExternalCaseRecordNumber(externalCaseRecordUuid);
		cs.setOrganization(org);
		this.makePersistent(cs);
	}

	public List<CaseRecord> findCaseRecordListByExternalCaseRecordNumber(
			String externalCaseRecordNummber) {
		Query q = getSession()
				.createQuery(
						"from CaseRecordTExternalCaseRecordNumber c where c.externalCaseRecordNumber = ? ");
		q.setString(0, externalCaseRecordNummber);
		return q.list();
	}
	/*
	 * TODO need to add classifiers to External Case Record Number table to be 
	 * able to distinguish one number from the other.  The current setup works
	 * assuming there is only one external agency, but when there are more than
	 * one agency, we'll need to revise the data model to include classification
	 * of the numbers.
	 */  
	public List<CaseRecordIdExternalCaseRecordNumber> findExternalCaseRecordNumbers(String caseRecordUuid) {
		String sql = "SELECT externalCaseRecordNumber" 
				+ " FROM CaseRecordIdExternalCaseRecordNumber "
				+ " WHERE caseRecord.uuid = ?";
		Query q = getSession().createQuery(sql);
		q.setString(0, caseRecordUuid);
		List l = q.list();
		Iterator i = l.iterator();
		ArrayList<CaseRecordIdExternalCaseRecordNumber> externalCaseRecordNumbers
			= new ArrayList<CaseRecordIdExternalCaseRecordNumber>();
		while (i.hasNext()) {
			//Object[] results = (Object[]) i.next();
			// String externalCaseRecordNumber = (String) results[0];
			 String externalCaseRecordNumber = (String) i.next();
			 CaseRecordIdExternalCaseRecordNumber crecrn = 
				 new CaseRecordIdExternalCaseRecordNumber(); 
			 crecrn.setExternalCaseRecordNumber(externalCaseRecordNumber);
			 externalCaseRecordNumbers.add(crecrn);
		}
		
		return externalCaseRecordNumbers;
			
	}

	public void updateExternalCaseNumber(String uuid,
			String newExternalCaseNumber) {
		CaseRecordIdExternalCaseRecordNumber externalId = (CaseRecordIdExternalCaseRecordNumber) getSession()
				.createQuery("from CaseRecordIdExternalCaseRecordNumber c where c.caseRecord.uuid = :id")
				.setParameter("id", uuid).uniqueResult();
		
		externalId.setExternalCaseRecordNumber(newExternalCaseNumber);
		getSession().update(externalId);
	}
}
